package com.coolgor.coldot.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryption {


    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];
        String s = new String(tempArr);
        return s;
    }


    public static String toSHA_1(String content){
        String result = null;
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(content.toString().getBytes());
            result = byteToStr(digest);
        } catch ( NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String toMD5(String content){
        String result = null;
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(content.toString().getBytes());
            result = byteToStr(digest);
        } catch ( NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String toHMAC(String content) {
        String result = null;
        return result;
    }


}
